package com.scu.phototabuser.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.scu.phototabuser.App;
import com.scu.phototabuser.R;

/**
 * Created by tianfei on 2017/5/3.
 */

public class WindowUtil {

    private static float fzoom = 0f;
    private static Context context= App.context;

    private static WindowManager manager;
    private static DisplayMetrics metrics;

    static {
        manager=(WindowManager) App.context.getSystemService(Context.WINDOW_SERVICE);
        metrics=new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
    }


    public static int getWindowWidthPx(){
        return metrics.widthPixels;
    }

    /**
     * 计算按照屏幕比例缩放后的尺寸
     */
    public static int zoom(Activity context, int size){
        if(fzoom == 0f){
            WindowManager windowManager = context.getWindowManager();
            Display display = windowManager.getDefaultDisplay();
            DisplayMetrics metrics = new DisplayMetrics();
            display.getMetrics(metrics);
            fzoom = metrics.density;
        }
        return (int)(size * fzoom);
    }

    public static int dpToPx(int dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

    public static int pxToDp(int px) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

}
